www.gusucode.com > VC 2D游戏编辑器-源码程序 > VC 2D游戏编辑器-源码程序/code/mapedit_source/map/Map.cpp

    //Download by http://www.NewXing.com
// Map.cpp: implementation of the CMap class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Map.h"
#include "..\\define.h"

DWORD SrcKey=DDBLTFAST_SRCCOLORKEY| DDBLTFAST_WAIT;
DWORD NoKey=DDBLTFAST_NOCOLORKEY| DDBLTFAST_WAIT;

LPDIRECTDRAW7 lpDD;
LPDIRECTDRAWSURFACE7  lpddsprimary;
LPDIRECTDRAWSURFACE7* DDS[4];
LPDIRECTDRAWSURFACE7  lpddData;
LPDIRECTDRAWSURFACE7  lpddSmallPic;
LPDIRECTDRAWSURFACE7  lpddPicCell;
LPDIRECTDRAWSURFACE7  lpddAlpha[3];

HWND				hwnd;
HRESULT				ghRet;

DWORD**				pS[5];
long*				SP[5];
long*				SW[5];
long*				SH[5];

extern RECT			rShowPic;
extern RECT			rLineRect;
extern RECT			client;
extern float		sw;
extern float		sh;
extern bool			IsZoomDown;
extern CPoint		g_ptScrollPos;
extern DWORD		dCurrentScreenX;
extern DWORD		dCurrentScreenY;
extern bool			IsFirstShowSPic;
extern int			iChangePic[DYNAMICPICNUMBER];

CMap::CMap()
{
	pObjData				=	NULL;
	pSurfaceData			=	NULL;
	pObjLink				=	NULL;
	pHeader.nMapVersion		=	VERSION;
	pHeader.nMapID			=	1;
	pHeader.nWidth			=	0;
	pHeader.nHeight			=	0;
	dCellNumber				=	0;

	pZoeticHeader.nZoeticVersion	=	VERSION;	//活物版本
	pZoeticHeader.nMagicN			=	0;			//活物数量
	pZoeticLink				=	NULL;
	dZoeticNumber			=	0;
}

CMap::CMap(WORD ID)
{
	pObjData				=	NULL;
	pSurfaceData			=	NULL;
	pObjLink				=	NULL;
	pHeader.nMapVersion		=	VERSION;
	pHeader.nMapID			=	ID;
	pHeader.nWidth			=	0;
	pHeader.nHeight			=	0;
	dCellNumber				=	0;

	pZoeticHeader.nZoeticVersion	=	VERSION;	//活物版本
	pZoeticHeader.nMagicN			=	0;			//活物数量
	pZoeticLink				=	NULL;
	dZoeticNumber			=	0;
}
CMap::~CMap()
{
	if (pObjData)
		delete[] pObjData;
	if (pSurfaceData)
		delete[] pSurfaceData;
	if(pObjLink)
	{
		delete[] pObjLink;
		pObjLink = NULL;
	}
	if(lpddData)
	{
		DD_SAFE_RELEASE(lpddData);
	}
	if (pZoeticLink)
		delete[] pZoeticLink;
}

void CMap::AddObj(WORD mapx, WORD mapy, _ObjCell cSelObj)
{
	if ((!pSurfaceData)||(!lpddData)||(!pObjData))
		return;
	_ObjLink* pLink = new _ObjLink;
	_ObjLink* pLink1;
	_ObjLink* pLink2;
	pLink->dx = mapx;
	pLink->dy = mapy;
	if(pObjLink == NULL)
	{
		pObjLink = pLink;
		pLink->Next = NULL;
		dCellNumber ++;
	}
	else
	{
		pLink1 = pObjLink;
		while((pLink1 != NULL)&&((pObjData[pLink1->dy*pHeader.nWidth+pLink1->dx].nRealY)*1024+pLink1->dx < cSelObj.nRealY*1024+mapx))
		{
			pLink2 = pLink1;
			pLink1 = pLink1->Next;
		}
		if(pLink1 == NULL)
		{
			pLink2->Next = pLink;
			pLink->Next = NULL;
			dCellNumber ++;
		}
		else if((pLink1->dx != pLink->dx)||(pLink1->dy != pLink->dy))
		{
			if(pLink1 == pObjLink)
			{
				pObjLink = pLink;
				pLink->Next = pLink1;
			}
			else
			{
				pLink2->Next = pLink;
				pLink->Next = pLink1;
			}
			dCellNumber ++;
		}
	}
	pObjData[mapy*pHeader.nWidth+mapx].nType = cSelObj.nType;	// set center texture
	pObjData[mapy*pHeader.nWidth+mapx].nID = cSelObj.nID;
	pObjData[mapy*pHeader.nWidth+mapx].nRealY = cSelObj.nRealY;
	mapx = (WORD)(mapx-dCurrentScreenX*(64-(client.right-client.left+31)/32));
	mapy = (WORD)(mapy-dCurrentScreenY*(64-(client.bottom-client.top+31)/32));
	if((mapx < 0)||(mapx > 64)||(mapy < 0)||(mapy > 64))
		return;
	if(cSelObj.nID == 255 || cSelObj.nType == 0)
		return;
	ShowAllObj( );
}

void CMap::AddSurface(WORD mapx, WORD mapy, _SurfaceCell cSelSurface, bool bObj)
{
	if ((!pSurfaceData)||(!lpddData)||(!pObjData))
		return;
	if(cSelSurface.nID == 255)
		return;
	pSurfaceData[mapy*pHeader.nWidth+mapx].nID = cSelSurface.nID;	// set center texture
	pSurfaceData[mapy*pHeader.nWidth+mapx].nHeight = cSelSurface.nHeight;
	pSurfaceData[mapy*pHeader.nWidth+mapx].nPass = cSelSurface.nPass;
	pSurfaceData[mapy*pHeader.nWidth+mapx].nEventType = cSelSurface.nEventType;
	pSurfaceData[mapy*pHeader.nWidth+mapx].nEvent = cSelSurface.nEvent;
	if(cSelSurface.nID < 21)
		pSurfaceData[mapy*pHeader.nWidth+mapx].nUseTranSition = cSelSurface.nUseTranSition;
	else
		pSurfaceData[mapy*pHeader.nWidth+mapx].nUseTranSition = 0;

	int imapx, imapy, j, i;
	imapx = mapx - dCurrentScreenX*(64-(client.right-client.left+31)/32);
	imapy = mapy - dCurrentScreenY*(64-(client.bottom-client.top+31)/32);
	if((imapx < 0)||(imapx > 64)||(imapy < 0)||(imapy > 64))
		return;
	long lPicW, lPicH;
	if(cSelSurface.nID < 21) 
	{
		lPicW = SW[0][cSelSurface.nID];
		lPicH = SH[0][cSelSurface.nID];
	}
	else
	{
		lPicW = 32;
		lPicH = SH[0][cSelSurface.nID];
	}
	if((lPicH/32 > 1)||(lPicW/32 > 1))
	{
		for(j = 0; j < lPicH/32; j++)
		{	
			for(i = 0; i < lPicW/32; i++)
			{
				if((i == 0)&&(j == 0))
					continue;
				pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nID = 255;	// set center texture
				pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nHeight = cSelSurface.nHeight;
				pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nEventType = cSelSurface.nEventType;
				pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nEvent = cSelSurface.nEvent;
			}
		}
	}
	else if(pSurfaceData[mapy*pHeader.nWidth+mapx].nUseTranSition)
	{
		HDC hdc_dec, hdc_sour, hdc_sour2;
		RECT rect;
		if (lpddData->GetDC(&hdc_dec) == DD_OK)
		{
			rect.left = imapx-1>0?imapx-1:0;
			rect.right = imapx+1<64?imapx+1:63;
			rect.top = imapy-1>0?imapy-1:0;
			rect.bottom = imapy+1<64?imapy+1:63;
			if(lpddAlpha[2]->GetDC(&hdc_sour) == DD_OK)
			{
				BitBlt(hdc_dec,rect.left*32, rect.top*32, (rect.right-rect.left+1)*32,(rect.bottom-rect.top+1)*32, hdc_sour, (rect.left-imapx+1)*32, (rect.top-imapy+1)*32, SRCAND);
				lpddAlpha[2]->ReleaseDC(hdc_sour);
			}
			for(i = 0; i < 3; i++)
				for(j = 0; j < 3; j ++)
					lpddAlpha[0]->BltFast(32*i, 32*j, DDS[0][cSelSurface.nID], NULL, NoKey);
			if(lpddAlpha[0]->GetDC(&hdc_sour2) == DD_OK)
			{
				if(lpddAlpha[1]->GetDC(&hdc_sour) == DD_OK)
				{
					BitBlt(hdc_sour2,0, 0, 3*32,3*32, hdc_sour, 0, 0, SRCAND);
					lpddAlpha[1]->ReleaseDC(hdc_sour);
				}
				BitBlt(hdc_dec,rect.left*32, rect.top*32, (rect.right-rect.left+1)*32,(rect.bottom-rect.top+1)*32, hdc_sour2, (rect.left-imapx+1)*32, (rect.top-imapy+1)*32, SRCPAINT);
				lpddAlpha[0]->ReleaseDC(hdc_sour2);
			}
			lpddData->ReleaseDC(hdc_dec);
		}
	}
	if(cSelSurface.nID < 21)
	{
		lpddData->BltFast(imapx*32, imapy*32, DDS[0][cSelSurface.nID], NULL, NoKey);
	}
	else
	{
		RECT rect;
		rect.left = iChangePic[cSelSurface.nID - 21]*32;
		rect.right = rect.left + 32;
		rect.top = 0;
		rect.bottom = lPicH;
		lpddData->BltFast(imapx*32, imapy*32, DDS[0][cSelSurface.nID], &rect, NoKey);
	}
	if(bObj)
		ShowAllObj( );
}

bool CMap::ResizeMap(WORD NewWidth, WORD NewHeight, _SurfaceCell cSelSurface)
{
	sw = NewWidth*32 / (float)(rShowPic.right-rShowPic.left);
	sh = NewHeight*32 / (float)(rShowPic.bottom-rShowPic.top);

	rLineRect.left = 0;
	rLineRect.top = 0;
	rLineRect.right = (long)((client.right-client.left)/sw);
	rLineRect.bottom = (long)((client.bottom-client.top)/sh);

	IsZoomDown = true;

	if (NewWidth == pHeader.nWidth && NewHeight == pHeader.nHeight)
		return false;
	if (NewWidth == 0 || NewHeight == 0)
	{
		pHeader.nHeight=pHeader.nWidth=0;
		delete[] pSurfaceData;
		delete[] pObjData;
		pSurfaceData=NULL;
		pObjData=NULL;
		if(lpddData)
		{
			DD_SAFE_RELEASE(lpddData);
		}
		if(pObjLink)
		{
			delete[] pObjLink;
			pObjLink = NULL;
		}
		if(pZoeticLink)
		{
			delete[] pZoeticLink;
			pZoeticLink = NULL;
			dZoeticNumber = 0;
		}
		SetMapSize(NewWidth,NewHeight);
	}
	else
	{
		if(lpddData)
		{
			DD_SAFE_RELEASE(lpddData);
		}
		DWORD dRealWidth, dRealHeight;
		dRealWidth = NewWidth > 64? 64:NewWidth;
		dRealHeight = NewHeight > 64? 64:NewHeight;
		DDSURFACEDESC2 ddsd;
		ZeroMemory(&ddsd,sizeof(ddsd));
		ddsd.dwSize = sizeof(ddsd);
		ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
		ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
		ddsd.dwHeight=dRealHeight*32; //高
		ddsd.dwWidth=dRealWidth*32; //宽

		ghRet = lpDD->CreateSurface( &ddsd, &lpddData, NULL );
		DD_ASSERT(ghRet,"Create Buffer");

		if ((pHeader.nWidth==0 && pHeader.nHeight==0) || (pSurfaceData==NULL && pObjData == NULL))
		{
			SetMapSize(NewWidth,NewHeight);
			pSurfaceData=new _SurfaceCell[NewWidth*NewHeight];
			pObjData = new _ObjCell[NewWidth*NewHeight];
			memset(pSurfaceData,0,NewWidth*NewHeight*sizeof(_SurfaceCell));
			memset(pObjData,0xffffffff,NewWidth*NewHeight*sizeof(_ObjCell));
		
			for (WORD j=0;j<NewHeight;j++)
				for (WORD i=0;i<NewWidth;i++)
				{
					AddSurface(i, j, cSelSurface);
				}
		}
		else
		{
			DWORD CopyWidth,CopyHeight, OldWidth, OldHeight;
			_SurfaceCell * pOldSurfaceData;
			_SurfaceCell cSurface;
			_ObjCell * pOldObjData;
			_ObjCell cObj;

			pOldSurfaceData = pSurfaceData;
			pOldObjData = pObjData;

			CopyWidth=NewWidth > pHeader.nWidth ? pHeader.nWidth : NewWidth;
			CopyHeight=NewHeight > pHeader.nHeight ? pHeader.nHeight : NewHeight;
			pSurfaceData = new _SurfaceCell[NewWidth*NewHeight];
			pObjData = new _ObjCell[NewWidth*NewHeight];

			OldWidth = pHeader.nWidth;
			OldHeight = pHeader.nHeight;

			SetMapSize(NewWidth,NewHeight);

			memset(pSurfaceData,0,NewWidth*NewHeight*sizeof(_SurfaceCell));
			memset(pObjData,255,NewWidth*NewHeight*sizeof(_ObjCell));

			WORD i, j;
			for (j=0;j<NewHeight;j++)
			{
				if (j<CopyHeight)
					for (i=0;i<NewWidth;i++)
					{
						if (i<CopyWidth)
						{
							if(!pOldSurfaceData[j*OldWidth+i].nUseTranSition)
							{
								cSurface.nID = pOldSurfaceData[j*OldWidth+i].nID;
								cSurface.nHeight = pOldSurfaceData[j*OldWidth+i].nHeight;
								cSurface.nPass = pOldSurfaceData[j*OldWidth+i].nPass;
								cSurface.nEvent = pOldSurfaceData[j*OldWidth+i].nEvent;
								cSurface.nEventType = pOldSurfaceData[j*OldWidth+i].nEventType;
								cSurface.nUseTranSition = 0;
								cSurface.res = pOldSurfaceData[j*OldWidth+i].res;
								AddSurface(i,j, cSurface, false);
							}			
						}
						else
						{
							cSelSurface.nUseTranSition = 0;
							AddSurface(i, j, cSelSurface, false);
						}
					}
				else
					for (i=0;i<NewWidth;i++)
					{
						cSelSurface.nUseTranSition = 0;
						AddSurface(i, j, cSelSurface, false);
					}
			}
			for(j = 0; j < CopyHeight; j ++)
				for(i = 0; i < CopyWidth; i++)
				{
					if(pOldSurfaceData[j*OldWidth+i].nUseTranSition)
					{
						cSurface.nID = pOldSurfaceData[j*OldWidth+i].nID;
						cSurface.nHeight = pOldSurfaceData[j*OldWidth+i].nHeight;
						cSurface.nPass = pOldSurfaceData[j*OldWidth+i].nPass;
						cSurface.nEvent = pOldSurfaceData[j*OldWidth+i].nEvent;
						cSurface.nEventType = pOldSurfaceData[j*OldWidth+i].nEventType;
						cSurface.nUseTranSition = 1;
						cSurface.res = pOldSurfaceData[j*OldWidth+i].res;
						AddSurface(i,j, cSurface, false);
					}
				}
			for(j = 0; j < CopyHeight; j ++)
				for(i = 0; i < CopyWidth; i++)
				{
					cObj.nID = pOldObjData[j*OldWidth+i].nID;
					cObj.nType = pOldObjData[j*OldWidth+i].nType;
					if((cObj.nID == 255)||(cObj.nType == 0))
						continue;
					cObj.nRealY = pOldObjData[j*OldWidth+i].nRealY;
					cObj.res = pOldObjData[j*OldWidth+i].res;
					AddObj(i, j, cObj);
				}
			delete[] pOldSurfaceData;
			delete[] pOldObjData;
		}
	}
	return true;
}

void CMap::SetMapSize(WORD width, WORD height)
{
	pHeader.nWidth=width;
	pHeader.nHeight=height;
}

bool CMap::LoadMap(char *szFileName)
{

	return true;
}

bool CMap::InitFail(char*msg)
{
	MsgBox(msg);
    DestroyWindow(hwnd);
    return false;
}


void CMap::ChangelpddData()
{
	WORD i, j;
	int iBase, number, iMaxX, iMaxY;
	iMaxX = pHeader.nWidth - dCurrentScreenX*(64-(client.right-client.left+31)/32) > 64?64:pHeader.nWidth - dCurrentScreenX*(64-(client.right-client.left+31)/32);
	iMaxY = pHeader.nHeight - dCurrentScreenY*(64-(client.bottom-client.top+31)/32) > 64?64:pHeader.nHeight - dCurrentScreenY*(64-(client.bottom-client.top+31)/32);
	iBase = dCurrentScreenY*(64-(client.bottom-client.top+31)/32)*pHeader.nWidth+dCurrentScreenX*(64-(client.right-client.left+31)/32);
	_SurfaceCell cSurface;
	for(j = 0; j < iMaxY; j++)
		for(i = 0; i < iMaxX; i++)
		{
			number = iBase+j*pHeader.nWidth+i;
			if(!pSurfaceData[number].nUseTranSition)
			{
				cSurface.nID = pSurfaceData[number].nID;
				cSurface.nPass =  pSurfaceData[number].nPass;
				cSurface.nHeight =  pSurfaceData[number].nHeight;
				cSurface.nEvent =  pSurfaceData[number].nEvent;
				cSurface.nEventType =  pSurfaceData[number].nEventType;
				cSurface.nUseTranSition =  pSurfaceData[number].nUseTranSition;
				cSurface.res =  pSurfaceData[number].res;
				AddSurface((WORD)(dCurrentScreenX*(64-(client.right-client.left+31)/32)+i), (WORD)(dCurrentScreenY*(64-(client.bottom-client.top+31)/32)+j), cSurface, false);
			}
		}
	for(j = 0; j < iMaxY; j++)
		for(i = 0; i < iMaxX; i++)
		{
			number = iBase+j*pHeader.nWidth+i;
			if(pSurfaceData[number].nUseTranSition)
			{
				cSurface.nID = pSurfaceData[number].nID;
				cSurface.nPass =  pSurfaceData[number].nPass;
				cSurface.nHeight =  pSurfaceData[number].nHeight;
				cSurface.nEvent =  pSurfaceData[number].nEvent;
				cSurface.nEventType =  pSurfaceData[number].nEventType;
				cSurface.nUseTranSition =  pSurfaceData[number].nUseTranSition;
				cSurface.res =  pSurfaceData[number].res;
				AddSurface((WORD)(dCurrentScreenX*(64-(client.right-client.left+31)/32)+i), (WORD)(dCurrentScreenY*(64-(client.bottom-client.top+31)/32)+j), cSurface, false);
			}
		}
 	ShowAllObj( );
}

void MsgBox(char*str,...)
{
	char*msg=new char[1024];
	va_list vl;
	va_start(vl,str);
	wvsprintf(msg,str,vl);
	va_end(vl);
	MessageBox(GetActiveWindow(),msg,"Message",MB_OK);
	delete [] msg;
	msg = NULL;
}

void CMap::ChangeData(WORD mapx, WORD mapy, int iHeight, BOOL bPass, int iEventType, int iEvent, int iRealY, int iStatus)
{
	int id;
	if ((!pSurfaceData)||(!lpddData)||(!pObjData))
		return;
	if(iStatus & 0x01)
		pSurfaceData[mapy*pHeader.nWidth+mapx].nHeight = iHeight;
	if(iStatus & 0x02)
		pSurfaceData[mapy*pHeader.nWidth+mapx].nPass = bPass;
	if(iStatus & 0x04)
		pSurfaceData[mapy*pHeader.nWidth+mapx].nEventType = iEventType;
	if(iStatus & 0x08)
		pSurfaceData[mapy*pHeader.nWidth+mapx].nEvent = iEvent;
		
	id = pObjData[mapy*pHeader.nWidth+mapx].nID;
	if((id == 255)||!(iStatus & 0x10)||(pObjData[mapy*pHeader.nWidth+mapx].nRealY == (DWORD)iRealY))
		return;
	_ObjLink*	pLink = pObjLink;
	_ObjLink*   pLink1;
	_ObjLink*   pLink2;
	while((pLink->dy != (DWORD)mapy)||(pLink->dx != (DWORD)mapx))
	{
		pLink1 = pLink;
		pLink = pLink->Next;
	}
	if(pLink == pObjLink)
	{
		if(pObjLink->Next == NULL)
		{
			pObjData[mapy*pHeader.nWidth+mapx].nRealY = iRealY;
			return;
		}
		else
			pObjLink = pObjLink->Next;
	}
	else
	{
		if(pLink->Next == NULL)
			pLink1->Next = NULL;
		else
			pLink1->Next = pLink->Next;
	}

	pLink1 = pObjLink;
	while((pLink1 != NULL)&&(pObjData[pLink1->dy*pHeader.nWidth+pLink1->dx].nRealY < (DWORD)iRealY))
	{
		pLink2 = pLink1;
		pLink1 = pLink1->Next;
	}
	if(pLink1 == NULL)
	{
		pLink2->Next = pLink;
		pLink->Next = NULL;
	}
	else if(pLink1 == pObjLink)
	{
		pObjLink = pLink;
		pLink->Next = pLink1;
	}
	else
	{
		pLink2->Next = pLink;
		pLink->Next = pLink1;
	}
	pObjData[mapy*pHeader.nWidth+mapx].nRealY = iRealY;
}
void CMap::DelObj(WORD mapx, WORD mapy)
{
	int type, id, i, j;
	id = pObjData[mapy*pHeader.nWidth+mapx].nID;
	type = pObjData[mapy*pHeader.nWidth+mapx].nType;
	if((id == 255)||(type == 0))
		return;
	pObjData[mapy*pHeader.nWidth+mapx].nID = 255;
	pObjData[mapy*pHeader.nWidth+mapx].nRealY = 0;
	pObjData[mapy*pHeader.nWidth+mapx].nType = 0;

	_ObjLink*	pLink = pObjLink;
	_ObjLink*   pLink2;
	while((pLink->dy != (DWORD)mapy)||(pLink->dx != (DWORD)mapx))
	{
		pLink2 = pLink;
		pLink = pLink->Next;
	}
	if(pLink == NULL)
		return;
	if(pLink == pObjLink)
	{
		if(pObjLink->Next == NULL)
			pObjLink = NULL;
		else
			pObjLink = pObjLink->Next;
	}
	else
	{
		if(pLink->Next == NULL)
			pLink2->Next = NULL;
		else
			pLink2->Next = pLink->Next;
	}
	delete[] pLink;
	dCellNumber --;

	int imapx, imapy;
	imapx = mapx - dCurrentScreenX*(64-(client.right-client.left+31)/32);
	imapy = mapy - dCurrentScreenY*(64-(client.bottom-client.top+31)/32);
	if((imapx < 0)||(imapx > 64)||(imapy < 0)||(imapy > 64))
		return;
	int sh = SH[type][id]/32;
	int sw = SW[type][id]/32;
	_SurfaceCell cSurface;
	for(j = -5; j < sh; j++)
		for(i = -5; i < sw; i++)
		{
			if((mapy+j < 0)||(mapx+i < 0))
				continue;
			if(!pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nUseTranSition)
			{
				cSurface.nID = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nID;
				if(cSurface.nID == 255)
					continue;
				cSurface.nPass = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nPass;
				cSurface.nEvent = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nEvent;
				cSurface.nEventType = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nEventType;
				cSurface.nHeight = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nHeight;
				cSurface.res = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].res;
				cSurface.nUseTranSition = 0;
				AddSurface(mapx+i, mapy+j, cSurface, false);
			}
		}
	for(j = -1; j < sh+1; j++)
		for(i = -1; i < sw+1; i++)
		{
			if((mapy+j < 0)||(mapx+i < 0))
				continue;
			if(pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nUseTranSition)
			{
				cSurface.nID = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nID;
				if(cSurface.nID == 255)
					continue;
				cSurface.nPass = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nPass;
				cSurface.nEvent = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nEvent;
				cSurface.nEventType = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nEventType;
				cSurface.nHeight = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].nHeight;
				cSurface.res = pSurfaceData[(mapy+j)*pHeader.nWidth+mapx+i].res;
				cSurface.nUseTranSition = 1;
				AddSurface(mapx+i, mapy+j, cSurface, false);
			}
		}
	ShowAllObj( );
}

void CMap::ShowAllObj( )
{
	int i, j, id, type;
	DWORD iStartX, iStartY;
	_ObjLink* pLink				= pObjLink;
	_ZoeticLink* pZoeticLink1	= pZoeticLink;
	iStartX = dCurrentScreenX*(64-(client.right-client.left+31)/32);
	iStartY = dCurrentScreenY*(64-(client.bottom-client.top+31)/32);
	while((pLink != NULL)||(pZoeticLink1 != NULL))
	{
		if((pLink != NULL)&&((pZoeticLink1 == NULL)||(pObjData[pLink->dy*pHeader.nWidth+pLink->dx].nRealY*1000+pLink->dx < pZoeticLink1->y*1000+pZoeticLink1->x)))
		{
			if((pLink->dy >= iStartY)&&(pLink->dy < iStartY+64)&&(pLink->dx >= iStartX)&&(pLink->dx < iStartX+64))
			{
				id = pObjData[pLink->dy*pHeader.nWidth+pLink->dx].nID;
				if(id == 255)
					break;
				type = pObjData[pLink->dy*pHeader.nWidth+pLink->dx].nType;
				if(type == 0)
					break;
				j = pLink->dy - iStartY;
				i = pLink->dx - iStartX;
				lpddData->BltFast(i*32, j*32, DDS[type][id], NULL, SrcKey);
			}
			pLink = pLink->Next;
		}
		else
		{
			if((pZoeticLink1->y >= iStartY)&&(pZoeticLink1->y <iStartY+64)&&(pZoeticLink1->x >= iStartX)&&(pZoeticLink1->x < iStartX+64))
			{
				type = 2;
				id = pZoeticLink1->dPicN;
				j = pZoeticLink1->y - iStartY;
				i = pZoeticLink1->x - iStartX;
				lpddData->BltFast(i*32, j*32, DDS[type][id], NULL, SrcKey);
			}
			pZoeticLink1 = pZoeticLink1->Next;
		}
	}
}

void CMap::DelAllObj( )
{
	memset(pObjData,0xffffffff,pHeader.nWidth*pHeader.nHeight*sizeof(_ObjCell));
	_ObjLink* pLink;
	pLink = pObjLink;
	while(pLink)
	{
		pObjLink = pLink;
		pLink = pObjLink->Next;
		delete[] pObjLink;
	}		
	pObjLink = NULL;
	dCellNumber = 0;
}

void CMap::RebuildObj( )
{
	_ObjLink* pLink;
	_ObjLink* pLink1;
	_ObjLink* pLink2;
	WORD i, j;
	pLink = pObjLink;
	while(pLink)
	{
		pObjLink = pLink;
		pLink = pObjLink->Next;
		delete[] pObjLink;
	}		
	pObjLink = NULL;
	dCellNumber = 0;
	for(i = 0; i < pHeader.nHeight; i++)
		for(j = 0; j < pHeader.nWidth; j++)
		{
			if((pObjData[i*pHeader.nWidth+j].nType != 0)&&(pObjData[i*pHeader.nWidth+j].nID != 255))
			{
				pLink = new _ObjLink;
				pLink->dx = j;
				pLink->dy = i;
				pLink->Next = NULL;
				if(dCellNumber == 0)
				{
					pObjLink = pLink;
					dCellNumber ++;
				}
				else
				{
					pLink1 = pObjLink;
					while((pLink1 != NULL)&&((pObjData[pLink1->dy*pHeader.nWidth+pLink1->dx].nRealY)*1024+pLink1->dx < (pObjData[i*pHeader.nWidth+j].nRealY)*1024+i))
					{
						pLink2 = pLink1;
						pLink1 = pLink1->Next;
					}
					if(pLink1 == NULL)
					{
						pLink2->Next = pLink;
						pLink->Next = NULL;
						dCellNumber ++;
					}
					else
					{
						if(pLink1 == pObjLink)
						{
							pObjLink = pLink;
							pLink->Next = pLink1;
						}
						else
						{
							pLink2->Next = pLink;
							pLink->Next = pLink1;
						}
						dCellNumber ++;
					}
				}
				pLink = NULL;
			}
		}
}

void CMap::DrawDynamicPic()
{
	int iBase, number, iMaxX, iMaxY, i, j;
	iMaxX = pHeader.nWidth - dCurrentScreenX*(64-(client.right-client.left+31)/32) > 64?64:pHeader.nWidth - dCurrentScreenX*(64-(client.right-client.left+31)/32);
	iMaxY = pHeader.nHeight - dCurrentScreenY*(64-(client.bottom-client.top+31)/32) > 64?64:pHeader.nHeight - dCurrentScreenY*(64-(client.bottom-client.top+31)/32);
	iBase = dCurrentScreenY*(64-(client.bottom-client.top+31)/32)*pHeader.nWidth+dCurrentScreenX*(64-(client.right-client.left+31)/32);
	_SurfaceCell cSurface;
	for(j = 0; j < iMaxY; j++)
		for(i = 0; i < iMaxX; i++)
		{
			number = iBase+j*pHeader.nWidth+i;
			cSurface.nID = pSurfaceData[number].nID;
			if((cSurface.nID > 20)&&(cSurface.nID != 255))
			{
				cSurface.nPass =  pSurfaceData[number].nPass;
				cSurface.nHeight =  pSurfaceData[number].nHeight;
				cSurface.nEvent =  pSurfaceData[number].nEvent;
				cSurface.nEventType =  pSurfaceData[number].nEventType;
				cSurface.nUseTranSition =  pSurfaceData[number].nUseTranSition;
				cSurface.res =  pSurfaceData[number].res;
				AddSurface((WORD)(dCurrentScreenX*(64-(client.right-client.left+31)/32)+i), (WORD)(dCurrentScreenY*(64-(client.bottom-client.top+31)/32)+j), cSurface, false);
			}
		}
}





















//------------------------------------活物函数--------------------------------------//
void CMap::AddZoetic(WORD	dID, DWORD x, DWORD y, WORD nPic)
{
	int i;
	char str[10];
	_ZoeticLink*	pZoeticLink1;
	_ZoeticLink*	pZoeticLink2;
	_ZoeticLink*	pZoeticLink3;
	if(!pSurfaceData[y*pHeader.nWidth+x].nPass)
		return;
	pZoeticLink1				= new _ZoeticLink;
	pZoeticLink1->dID			= dID;
	pZoeticLink1->dType			= 0;
	itoa(dID, str, 10);
	strcpy(pZoeticLink1->dName, str);
	pZoeticLink1->dPicN			= nPic;
	pZoeticLink1->x				= x;
	pZoeticLink1->y				= y;
	pZoeticLink1->dEvent		= -1;
	pZoeticLink1->dArea			= 5;
	pZoeticLink1->dCharacter	= 0;
	pZoeticLink1->v				= 100;
	pZoeticLink1->speed			= 3;
	pZoeticLink1->health		= 100;
	pZoeticLink1->MaxHealth		= 100;
	pZoeticLink1->vigor			= 100;
	pZoeticLink1->MaxVigor		= 100;
	pZoeticLink1->energy		= 100;
	pZoeticLink1->MaxEnergy		= 100;
	pZoeticLink1->defend		= 10;
	pZoeticLink1->attack		= 10;
	pZoeticLink1->agility		= 10;
	pZoeticLink1->FollowID		= -1;
	pZoeticLink1->Bmoney		= 1000;
	pZoeticLink1->MaxExperiment = 10000;
	for(i = 0; i < Max_SKILL_NPC; i++)
	{
		pZoeticLink1->skill[i].id		= -1;
		pZoeticLink1->skill[i].level	= 0;
	}
	for(i = 0; i < Max_GOODS_NPC; i++)
	{
		pZoeticLink1->medica_goods[i].id		= -1;
		pZoeticLink1->medica_goods[i].iNumber	= 0;
	}
	for(i = 0; i < 6; i++)
	{
		pZoeticLink1->equiped[i]	= -1;
	}
	if(pZoeticLink == NULL)
	{
		pZoeticLink = pZoeticLink1;
		pZoeticLink->Back = NULL;
		pZoeticLink->Next = NULL;
		dZoeticNumber ++;
	}
	else
	{
		pZoeticLink2 = pZoeticLink;
		while((pZoeticLink2 != NULL)&&(pZoeticLink2->y*1000+pZoeticLink->x < pZoeticLink1->y*1000+pZoeticLink->x))
		{
			pZoeticLink3 = pZoeticLink2;
			pZoeticLink2 = pZoeticLink2->Next;
		}
		if(pZoeticLink2 == NULL)
		{
			pZoeticLink3->Next = pZoeticLink1;
			pZoeticLink1->Back = pZoeticLink3;
			pZoeticLink1->Next = NULL;
			dZoeticNumber ++;
		}
		else if((pZoeticLink2->x != pZoeticLink1->x)||(pZoeticLink2->y != pZoeticLink1->y))
		{
			if(pZoeticLink2 == pZoeticLink)
			{
				pZoeticLink = pZoeticLink1;
				pZoeticLink->Next = pZoeticLink2;
				pZoeticLink2->Back = pZoeticLink1;
				pZoeticLink->Back = NULL;
			}
			else
			{
				pZoeticLink3->Next = pZoeticLink1;
				pZoeticLink1->Back = pZoeticLink3;
				pZoeticLink1->Next = pZoeticLink2;
				pZoeticLink2->Back = pZoeticLink1;
			}
			dZoeticNumber ++;
		}
		else
		{
			if(pZoeticLink2 == pZoeticLink)
			{
				pZoeticLink = pZoeticLink1;
				pZoeticLink1->Back = NULL;
				pZoeticLink1->Next = pZoeticLink2->Next;
				pZoeticLink2->Back = pZoeticLink1;
			}
			else
			{
				pZoeticLink3->Next = pZoeticLink1;
				pZoeticLink1->Back = pZoeticLink3;
				pZoeticLink1->Next = pZoeticLink2->Next;
				pZoeticLink2->Next->Back = pZoeticLink1;
			}
			delete[] pZoeticLink2;
		}
	}
	pZoeticLink1 = NULL;
	pZoeticLink2 = NULL;
	pZoeticLink3 = NULL;
	ShowAllObj( );
}

void CMap::ChangeZoeticInfo(WORD	dID,		WORD	dType,			char * dName,			WORD	dPicN,			DWORD	x,				DWORD	y,			WORD	dEvent,		DWORD	dArea,		WORD	dCharacter,	WORD	v,			WORD	speed,	
							WORD	health,		WORD	MaxHealth,		WORD	vigor,			WORD	MaxVigor,		WORD	energy,			WORD	MaxEnergy,	WORD	defend,		WORD	attack,		WORD	agility,	WORD	FollowID,	
							WORD	Bmoney,		DWORD	MaxExperiment,	SKILL *	skill,			GOODS *	medica_goods,	WORD  *	equiped,		DWORD	iStatus)
{
	int i;
	_ZoeticLink*	pZoeticLink1 = pZoeticLink; 
	while((pZoeticLink1 != NULL)&&(pZoeticLink1->dID != dID))
	{
		pZoeticLink1 = pZoeticLink1->Next;
	}
	if(pZoeticLink1 == NULL)
	{
		AfxMessageBox("无此dID的活物,请核对后在试一次",MB_OK);
		return;
	}
	if(iStatus & 0x1)
		pZoeticLink1->agility		= agility;
	if(iStatus & 0x2)
		pZoeticLink1->dArea			= dArea;
	if(iStatus & 0x4)
		pZoeticLink1->attack		= attack;
	if(iStatus & 0x8)
		pZoeticLink1->dCharacter	= dCharacter;
	if(iStatus & 0x10)
		pZoeticLink1->defend		= defend;
	if(iStatus & 0x20)
		pZoeticLink1->energy		= energy;
	if(iStatus & 0x40)
		for(i = 0; i < 6; i++)
		{
			pZoeticLink1->equiped[i]	= equiped[i];
		}
	if(iStatus & 0x80)
		pZoeticLink1->dEvent		= dEvent;
	if(iStatus & 0x100)
		pZoeticLink1->FollowID		= FollowID;
	if(iStatus & 0x200)
		for(i = 0; i < Max_GOODS_NPC; i++)
		{
			pZoeticLink1->medica_goods[i].id		= medica_goods[i].id;
			pZoeticLink1->medica_goods[i].iNumber	= medica_goods[i].iNumber;
		}
	if(iStatus & 0x400)
		pZoeticLink1->health		= health;
	if(iStatus & 0x800)
		pZoeticLink1->MaxEnergy		= MaxEnergy;
	if(iStatus & 0x1000)
		pZoeticLink1->MaxExperiment = MaxExperiment;
	if(iStatus & 0x2000)
		pZoeticLink1->MaxHealth		= MaxHealth;
	if(iStatus & 0x4000)
		pZoeticLink1->MaxVigor		= MaxVigor;
	if(iStatus & 0x8000)
		pZoeticLink1->Bmoney		= Bmoney;
	if(iStatus & 0x10000)
		pZoeticLink1->dPicN			= dPicN;
	if(iStatus & 0x20000)
		for(i = 0; i < Max_SKILL_NPC; i++)
		{
			pZoeticLink1->skill[i].id		= skill[i].id;
			pZoeticLink1->skill[i].level	= skill[i].level;
		}
	if(iStatus & 0x40000)
		pZoeticLink1->speed			= speed;
	if(iStatus & 0x80000)
		pZoeticLink1->dType			= dType;
	if(iStatus & 0x100000)
		pZoeticLink1->v				= v;
	if(iStatus & 0x200000)
		pZoeticLink1->vigor			= vigor;
	if(iStatus & 0x400000)
		pZoeticLink1->x				= x;
	if(iStatus & 0x800000)
		pZoeticLink1->y				= y;
	if(iStatus & 0x1000000)
		strcpy(pZoeticLink1->dName, dName);
	pZoeticLink1 = NULL;
}

void CMap::DelZoetic(DWORD x, DWORD y)
{
	_ZoeticLink*	pZoeticLink1 = pZoeticLink; 
	while((pZoeticLink1 != NULL)&&((pZoeticLink1->x != x)||(pZoeticLink1->y != y)))
	{
		pZoeticLink1 = pZoeticLink1->Next;
	}
	if(pZoeticLink1 == NULL)
	{
		AfxMessageBox("没找到这个活物,请核对后在试一次",MB_OK);
		return;
	}
	else
	{
		if(pZoeticLink1 == pZoeticLink)
		{
			if(	pZoeticLink1->Next == NULL)
			{
				pZoeticLink  = NULL;
			}
			else
			{
				pZoeticLink = pZoeticLink->Next;
				pZoeticLink->Back = NULL;
			}
		}
		else
		{
			if(pZoeticLink1->Next == NULL)
			{
				pZoeticLink1->Back->Next = NULL;
			}
			else
			{
				pZoeticLink1->Back->Next = pZoeticLink1->Next;
				pZoeticLink1->Next->Back = pZoeticLink1->Back;
			}
		}
		delete pZoeticLink1;
	}
	pZoeticLink1 = NULL;
	dZoeticNumber --;

	int imapx, imapy;
	imapx = x - dCurrentScreenX*(64-(client.right-client.left+31)/32);
	imapy = y - dCurrentScreenY*(64-(client.bottom-client.top+31)/32);
	if((imapx < 0)||(imapx > 64)||(imapy < 0)||(imapy > 64))
		return;
	int sh = 5;
	int sw = 5;
	int i, j;
	_SurfaceCell cSurface;
	for(j = -5; j < sh; j++)
		for(i = -5; i < sw; i++)
		{
			if(((int)(y+j) < 0)||((int)(x+i) < 0))
				continue;
			if(!pSurfaceData[(y+j)*pHeader.nWidth+x+i].nUseTranSition)
			{
				cSurface.nID = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nID;
				if(cSurface.nID == 255)
					continue;
				cSurface.nPass = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nPass;
				cSurface.nEvent = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nEvent;
				cSurface.nEventType = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nEventType;
				cSurface.nHeight = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nHeight;
				cSurface.res = pSurfaceData[(y+j)*pHeader.nWidth+x+i].res;
				cSurface.nUseTranSition = 0;
				AddSurface((WORD)(x+i), (WORD)(y+j), cSurface, false);
			}
		}
	for(j = -1; j < sh+1; j++)
		for(i = -1; i < sw+1; i++)
		{
			if((y+j < 0)||(x+i < 0))
				continue;
			if(pSurfaceData[(y+j)*pHeader.nWidth+x+i].nUseTranSition)
			{
				cSurface.nID = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nID;
				if(cSurface.nID == 255)
					continue;
				cSurface.nPass = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nPass;
				cSurface.nEvent = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nEvent;
				cSurface.nEventType = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nEventType;
				cSurface.nHeight = pSurfaceData[(y+j)*pHeader.nWidth+x+i].nHeight;
				cSurface.res = pSurfaceData[(y+j)*pHeader.nWidth+x+i].res;
				cSurface.nUseTranSition = 1;
				AddSurface((WORD)(x+i), (WORD)(y+j), cSurface, false);
			}
		}
	ShowAllObj( );
}

void CMap::DelAllZoetic( )
{
	_ZoeticLink*	pZoeticLink1 = pZoeticLink; 
	_ZoeticLink*	pZoeticLink2;
	while(pZoeticLink1 != NULL)
	{
		pZoeticLink2 = pZoeticLink1;
		pZoeticLink1 = pZoeticLink1->Next;
		delete[] pZoeticLink2;
	}
	pZoeticLink  = NULL;
	pZoeticLink1 = NULL;
	pZoeticLink2 = NULL;
	dZoeticNumber = 0;
}